﻿using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class ShoppingService
    {
        /// <summary>
        /// 根据客户id查找购物车信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public static List<Shopping> GetShoppings(int Id)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                return db.Shopping.Where(p=>p.CustId==Id).ToList();
            }
        }
        /// <summary>
        /// 获取购物车信息
        /// </summary>
        /// <param name="limit"></param>
        /// <param name="page"></param>
        /// <param name="Name"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public static List<Shopping> GetShoppMsg(int limit, int page, string Name, out int total)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                var query = db.Shopping.Where(p => true);
                //if (!string.IsNullOrEmpty(Name))
                //{
                //    query = query.Where(p => p.CustId==Name);
                //}
                total = query.Count();
                return query.OrderBy(p => p.Id).Skip((page - 1) * limit).Take(limit).ToList();
            }
        }


        /// <summary>
        /// 向购物车添加数据
        /// </summary>
        /// <param name="shopping"></param>
        /// <returns></returns>
        public static int AddShopping(Shopping shopping)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                db.Shopping.Add(shopping);
                return db.SaveChanges();
            }
        }

        /// <summary>
        /// 根据id删除购物车商品信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public static int Delete(int Id)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                Shopping shopping = new Shopping() { Id = Id };

                db.Shopping.Attach(shopping);
                db.Shopping.Remove(shopping);
                return db.SaveChanges();
            }
        }
    }
}
